#include <iostream>
#include <fstream>

#include "state.hpp"
#include "conversion.hpp"

using namespace std;

char *paste(int);
char *paste(const char *, int, int, int);

int main(int argc, char** argv)
{
    complexd h[] = {
        complexd(1/sqrt(2), 0), complexd( 1/sqrt(2), 0),
        complexd(1/sqrt(2), 0), complexd(-1/sqrt(2), 0)
    };

    Conversion H(2,2,h);

    int init, rank, size;
    MPI_Initialized(&init);
    if (!init)
        MPI_Init(&argc, &argv);
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);
    MPI_Comm_size(MPI_COMM_WORLD, &size);

    for (int n = 3; n <= 23; n += 5) {
        State state(n), state_sample(n);

        sample = paste(n);
        ifstream in(sample);
        in >> state_sample;
        in.close();
        ofstream out;

        res = paste(dir, n, 1, size);
        out.open(res);
        state = state_sample;
        state.doConversion(H, 1);
        out << state;
        out.close();

        res = paste(dir, n, n/2 + 1, size);
        out.open(res);
        state = state_sample;
        state.doConversion(H, n/2);
        out << state;
        out.close();

        res = paste(dir, n, n, size);
        out.open(res);
        state = state_sample;
        state.doConversion(H, n);
        out << state;
        out.close();
    }
    return 0;
}
